package com.yanqu.road.server.cmd.coldRoom;

import com.yanqu.road.entity.player.UserBeauty;
import com.yanqu.road.entity.player.UserColdRoomSite;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.pb.ColdRoomPb;
import com.yanqu.road.pb.coldRoom.ColdRoomProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.BeautyModule;
import com.yanqu.road.server.gameplayer.module.player.ColdRoomModule;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_COLDROOM_DISPATCH_BEAUTY, desc = "派遣(召回)美女")
public class DispatchBeautyCmd extends AbstractCommand{

    private int beautyId;
    private int siteIndex;
    private long harvest;
    private UserColdRoomSite site;
    private long nowTime;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        ColdRoomProto.DispatchBeautyReqMsg reqMsg = ColdRoomProto.DispatchBeautyReqMsg.parseFrom(packet.getBytes());
        beautyId = reqMsg.getBeautyId();
        siteIndex = reqMsg.getSiteIndex();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        site = player.getModule(ColdRoomModule.class).getUserColdRoomSite(siteIndex);
        nowTime = System.currentTimeMillis();
        if (site == null) {
            ret = GameErrorCode.E_COLDROOM_NOT_FIND_SITEINDEX;
            return;
        }
        if (beautyId > 0) {
            // 派遣
            if (site.getBeautyId() != 0) {
                ret = GameErrorCode.E_COLDROOM_DISPATCH_STATUS_ERROR;
                return;
            }
            int beautyCount = 0;
            int freeBeautyCount = 0;
            for (UserBeauty beauty : player.getModule(BeautyModule.class).getBeautyMap().values()) {
                if (beauty == null) {
                    continue;
                }
                beautyCount++;
                if (player.getModule(ColdRoomModule.class).isBeautyFree(beauty.getBeautyId())) {
                    freeBeautyCount++;
                }
            }
            if (beautyCount < GameConfig.COLDROOM_UNLOCK_BEAUTY_NUM) {
                ret = GameErrorCode.E_BEAUTY_COUNT_NO_ENOUGH;
                return;
            }
            if (freeBeautyCount <= GameConfig.COLDROOM_FREE_BEAUTY_NUM) {
                ret = GameErrorCode.E_BEAUTY_FREE_COUNT_NOT_ENOUGH;
                return;
            }
            if (!player.getModule(ColdRoomModule.class).isBeautyFree(beautyId)) {
                ret = GameErrorCode.E_BEAUTY_HAS_NOT_TIME;
                return;
            }

            UserBeauty userBeauty = player.getModule(BeautyModule.class).getUserBeauty(beautyId);
            if (userBeauty == null) {
                ret = GameErrorCode.E_BEAUTY_NO_FOUND;
                return;
            }
            player.getModule(ColdRoomModule.class).dispatchBeauty(site, beautyId, nowTime);
        } else {
            // 召回
            if (site.getBeautyId() == 0) {
                ret = GameErrorCode.E_COLDROOM_CALLBACK_STATUS_ERROR;
                return;
            }
            harvest = player.getModule(ColdRoomModule.class).callBackBeauty(site, nowTime);

        }
    }

    @Override
    public void sendData(GamePlayer player) {
        ColdRoomProto.DispatchBeautyRespMsg.Builder respMsg = ColdRoomProto.DispatchBeautyRespMsg.newBuilder();
        respMsg.setRet(ret);
        if(0 == ret){
            respMsg.setHarvest(harvest);
        }
        if (site != null) {
            respMsg.setSiteInfo(ColdRoomPb.parseColdRoomSiteTempMsg(site));
        }
        player.sendPacket(Protocol.U_COLDROOM_DISPATCH_BEAUTY, respMsg);
    }
}
